#!/bin/bash

# 为已存在的用户设置服务
for user_home in /home/*; do
    if [ -d "$user_home" ]; then
        username=$(basename "$user_home")
        if id "$username" >/dev/null 2>&1; then
            sudo -u "$username" mkdir -p "$user_home/.config/systemd/user"
            sudo -u "$username" mkdir -p "$user_home/.local/bin"
            # 从 /etc/skel 复制模板文件
            sudo -u "$username" cp /etc/skel/.config/systemd/user/fcitx5-monitor.service "$user_home/.config/systemd/user/" 2>/dev/null || true
            sudo -u "$username" cp /etc/skel/.local/bin/fcitx5-monitor.sh "$user_home/.local/bin/" 2>/dev/null || true
            sudo -u "$username" chmod +x "$user_home/.local/bin/fcitx5-monitor.sh"
        fi
    fi
done

# 启用用户服务
sudo -u "$username" systemctl --user daemon-reload
sudo -u "$username" systemctl --user enable fcitx5-monitor.service
sudo -u "$username" systemctl --user start fcitx5-monitor.service
sudo -u "$username" systemctl --user restart fcitx5-monitor.service
echo "请使用以下命令启用用户服务:"
echo "systemctl --user daemon-reload"
echo "systemctl --user enable fcitx5-monitor.service"
echo "systemctl --user start fcitx5-monitor.service"
echo "systemctl --user restart fcitx5-monitor.service"

exit 0